package com.fiberhome.nas.core.model.request

import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File


/**
 * 上传参数封装类
 */
data class UploadRequest(
    // 通用参数
    val fileName: String,
    val fileCreatedAt: String,
    val fileModifiedAt: String,
    val chunkOffset: Long,
    val chunkSize: Long,
    val chunkNumber: Int,
    val chunkHash: String,
    val totalChunk: Int,
    val totalSize: Long,
    val fileHash: String,
    val hashAlgorithm: String = "MD5",
    val assetData: File,

    val path: String? = null,   //文件上传使用
    val deviceAssetId: String? = null,  //媒体上传使用
    val deviceId: String? = null,   //媒体上传使用
    val isFavorite: Boolean? = null,    //媒体上传使用
    val duration: String? = null    //媒体上传使用
) {
    // 转换为字符串的辅助属性
    val chunkOffsetStr: String get() = chunkOffset.toString()
    val chunkSizeStr: String get() = chunkSize.toString()
    val chunkNumberStr: String get() = chunkNumber.toString()
    val totalChunkStr: String get() = totalChunk.toString()
    val totalSizeStr: String get() = totalSize.toString()
    val isFavoriteStr: String? get() = isFavorite?.toString()

    /**
     * 创建文件数据部分
     */
    fun createAssetDataPart(): MultipartBody.Part {
        return MultipartBody.Part.createFormData(
            name = "assetData",
            filename = fileName,
            body = RequestBody.create(
                "multipart/form-data".toMediaTypeOrNull(),
                assetData
            )
        )
    }
}